#ifndef DIALOG_H
#define DIALOG_H

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "dialog.h"
#include "ui_dialog.h"
#include<QMessageBox>
#define Columns 16 //20鍒?
#define Rows 16  //20琛?
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#define NOKEY 0
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QDialog>
#include<QPalette>
#include<QPushButton>
#include<QLabel>
#include<QPainter>
#include<QDebug>
#include<QKeyEvent>
#include<QTimer>
#include<QTime>
#include <QThread>
#include <QMainWindow>
#include <QThread>
#include <QDebug>
#include <QPushButton>
namespace Ui {
class Dialog;
}
class WorkerThread;

class Dialog : public QDialog
{
    Q_OBJECT
public slots:
    void timeOut();
    void btnStartslot();
    void btnPauseslot();
    void btnWestlot();
    void btnEastlot();
    void btnSouthlot();
    void btnNorthlot();

protected:
    void keyPressEvent(QKeyEvent *);
    void paintEvent(QPaintEvent *event);
private:
    QPushButton *pushButton;
    WorkerThread *workerThread ;
private slots:
    void handleResults(const QString &result);
/* 点击按钮开启线程 */
     void pushButtonClicked();
public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    void LedDianzhen();
    void showLed();
    void ControlLed();
    void initialized();
    void snakeRun();
    void checkHItWall();
    void checkEatSelf();
    enum  Direction:int{
        East,
        South,
        West,
        North
    };
private:
    char GB_16[32];//点阵
    int food[2];//食物坐标
    int snake[100][2];//蛇坐标

    int led[16][16];
    int s[32];
    int num;
    int eatfoodCount;//吃到的食物总数
    int directtion;//蛇头方向
    int scores;//得分
    int speed;//蛇移动速度
    QTableWidget *table;
    QLabel *lbScroe;
    QTimer *timer;
    Ui::Dialog *ui;
};
class WorkerThread : public QThread
 {
 /* 用到信号槽即需要此宏定义 */
 Q_OBJECT

 public:
    WorkerThread(QWidget *parent = nullptr) {
    Q_UNUSED(parent);
    }

/* 重写 run 方法，继承 QThread 的类，只有 run 方法是在新的线程里 */
    void run() override {
    QString result = "线程开启成功";
    usleep(100000);
    struct input_event t;
    int keys_fd = open("/dev/input/event4", O_RDONLY);
    int directtion;
    if(read(keys_fd,&t,sizeof(t))==sizeof(t)) {
    if(t.type==EV_KEY)
    {
       if(t.code== 3 && t.value==1) directtion=2;//North;
       if(t.code== 6 && t.value==1) directtion=5;//South;
        if(t.code== 5 && t.value==1) directtion=6;//East;
       if(t.code== 7 && t.value==1) directtion=4;//West;

    }
          }
    result =QString::number(directtion);

    close(keys_fd);
     emit resultReady(result);
    }

 signals:

 void resultReady(const QString &s);

 };

#endif // DIALOG_H

